<template>
	<view class="content">
		<uni-search-bar class="search" placeholder="输入关键词进行搜索" @focus="handleSearch" cancel-text="cancel">
			<uni-icons slot="searchIcon" color="#999999" size="18" type="home" />
		</uni-search-bar>
		<swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000">
			<swiper-item v-for="item in swipers" :key="index">
				<view class="swiper-item" :style="{backgroundColor: item}"></view>
			</swiper-item>
		</swiper>
		<view class="">
			<view v-for="(item,index) in list" :key="item.id" class="item" @click="handleClick(item.id)">
				<view class="imageWrap">
					<image :src="item.image" mode="" class="image"></image>
				</view>
				<view class="itemDetail">
					<view class="title">{{item.title}}</view>
					<view class="price">¥ {{item.price}}</view>
				</view>
				
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				swipers: ['red', 'green', 'blue', 'yellow', 'pink'],
				page: 1,
				list: []
			}
		},
		onReachBottom() {
			this.page += 1;
			this.fetchList();
		},
		onPullDownRefresh() {
			this.list = []
			this.page = 1;
			this.fetchList()
			
		},
		onLoad() {
			this.fetchList()
		},
		methods: {
			handleSearch() {
				uni.navigateTo({
					url: '/pages/search/search'
				})
			},
			fetchList() {
				uni.request({
					url: 'http://localhost:3000/list',
					data: {
						_limit: 8,
						_page: this.page
					}
				}).then(res => {
					this.list = this.page === 1?res.data:this.list.concat(res.data)
				})
			},
			handleClick(id) {
				uni.navigateTo({
					url:'/pages/detail/detail?id=' + id
				})
			}
		}
	}
</script>

<style lang="scss">
	.search {
		width: 750rpx;
	}
	.swiper-item {
		height: 350rpx;
	}
	.item {
		display: flex;
		height: 200rpx;
		margin: 20rpx auto 20rpx;
		width: 702rpx;
		flex-direction: row;
		.imageWrap {
			width: 200rpx;
			height: 200rpx;
			border-radius: 20rpx;
			overflow: hidden;
			flex-shrink: 0;
			.image {
				width: 200rpx;
				height: 200rpx;
			}
		}
		.itemDetail {
			margin-left: 20rpx;
			.title {
				font-size: 34rpx;
			}
			.price {
				font-size: 28rpx;
				color: $uni-color-success;
			}
		}
	}
</style>